<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CompanyWater extends \app\BaseModel
{
    protected $name = "massage_company_water";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function addWaterQueue($uniacid, $comm_id = 0, $i = 1)
    {
        if(!empty($comm_id)) {
            cacheLpush("company_water_success", $comm_id, $uniacid);
        }
        $comm_model = new Commission();
        $num = 0;
        while ($num < $i) {
            $num++;
            $data_id = cacheLpop("company_water_success", $uniacid);
            if(!empty($data_id)) {
                $comm = $comm_model->dataInfo(["status" => 1, "id" => $data_id]);
                if(!empty($comm)) {
                    $res = $this->updateCash($uniacid, 0, $comm["company_cash"], 1);
                    if($res == 0) {
                        cacheLpush("company_water_success", $data_id, $uniacid);
                    }
                    $res = $comm_model->dataUpdate(["status" => 1, "id" => $data_id], ["status" => 2, "cash_time" => time()]);
                    if($res == 0) {
                        cacheLpush("company_water_success", $data_id, $uniacid);
                    }
                }
            } else {
                return true;
            }
        }
        return true;
    }
    public function updateCash($uniacid, $user_id, $cash, $is_add)
    {
        $is_add = $is_add == 1 ? $is_add : -1;
        $admin_user_model = new \app\adminuser\model\AdminUser();
        $admin_info = $admin_user_model->dataInfo(["uniacid" => $uniacid], 1);
        $last_record = $this->where(["uniacid" => $uniacid])->order("id desc")->find();
        $admin_cash = $admin_info["cash"];
        if(!empty($last_record) && round($last_record->after_cash, 2) != round($admin_cash, 2)) {
            return 0;
        }
        $true_cash = $is_add == 1 ? $cash : $cash * -1;
        if($true_cash == 0) {
            return true;
        }
        $res = $admin_user_model->where(["id" => $admin_info["id"], "cash" => $admin_cash])->update(["cash" => \think\facade\Db::Raw("cash+" . $true_cash)]);
        if($res == 0) {
            return 0;
        }
        $insert = ["uniacid" => $uniacid, "user_id" => $admin_info["id"], "before_cash" => $admin_cash, "after_cash" => $admin_cash + $true_cash, "cash" => $cash, "res" => $res, "create_time" => time(), "add" => $is_add];
        $res = $this->insert($insert);
        if($res == 0) {
            return 0;
        }
        return $res;
    }
}

?>